<template>
    <view class="content">
        <u--form labelPosition="left" labelWidth="70" :model="userInfo" :rules="rules" ref="form1">
            <u-form-item label="回访客户" prop="nickname" @click="show = true">
                <u--input v-model="userInfo.nickname" disabled disabledColor="#ffffff" placeholder="请选择回访客户"
                    suffixIcon="arrow-down">
                </u--input>
            </u-form-item>
            <u-form-item label="回访内容" prop="content">
                <u--textarea v-model="userInfo.content" placeholder="请输入内容" count confirmType="done"></u--textarea>
            </u-form-item>
        </u--form>
        <u-button @click="submit" color="#19BC9C">添加回访内容</u-button>
        <u-picker :show="show" :columns="BusinessList" keyName="nickname" @cancel="show = false"
            @confirm="confirm"></u-picker>
        <u-toast ref="uToast"></u-toast>
    </view>
</template>
<script>
export default {
    data() {
        return {
            show: false,
            userInfo: {
                content: '',
                busid: null,
                nickname: ''
            },
            columns: [],
            rules: {
                'content': {
                    type: 'string',
                    required: true,
                    message: '请填写回访内容',
                    trigger: ['blur', 'change']
                },
                'nickname': {
                    type: 'string',
                    required: true,
                    message: '请选择回访客户',
                    trigger: ['blur', 'change']
                },
            },
            LoginUser: {},
            BusinessList: []
        }
    },
    onReady()
    {
        // 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
        this.$refs.form1.setRules(this.rules)

        this.$u.auth.check()

        this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {}

        this.getBusinessData()
    },
    methods: {
        async getBusinessData()
        {
            let result = await this.$u.api.business.businesslist({adminid:this.LoginUser.id})

            if(result.code === 1)
            {
                this.BusinessList.push(result.data)
            }else{
                this.$refs.uToast.show({
                    type:'error',
                    message:result.msg,
                    complete:() => {
                        this.$u.route({
                            url:'/packageA/visit/index'
                        })
                    }
                })
            }
        },
        confirm(e)
        {
            let value = e.value[0]

            this.userInfo.nickname = value.nickname

            this.userInfo.busid = value.id

            this.show = false
        },
        submit()
        {
            this.$refs.form1.validate().then(async (res) => {
                let data = {
                    busid:this.userInfo.busid,
                    adminid:this.LoginUser.id,
                    content:this.userInfo.content
                }

                let result = await this.$u.api.business.add(data)

                if(result.code === 1)
                {
                    this.$refs.uToast.show({
                        type:'success',
                        message:result.msg,
                        complete:() => {
                            this.$u.route({
                                url:'/packageA/visit/index'
                            })
                        }
                    })

                    return false
                }else{
                    this.$refs.uToast.show({
                        type:'error',
                        message:result.msg
                    })
                    return false
                }
            }).catch((err) => {
                
            });
        }
    },
}
</script>

<style>
	.content{
		width: 90%;
		height: 100vh;
		margin: 0 auto;
	}
</style>